package com.auto.survey.activity.monitoring

import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_OBJECT_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_PROJET_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_STATION_ID
import com.auto.survey.activity.monitoring.adapter.CollapsiblePointAdapter
import com.auto.survey.activity.monitoring.dao.MonitoringRepository
import com.auto.survey.activity.monitoring.entity.UiPointWithCheckPoint
import com.auto.survey.base.BaseActivity
import kotlinx.coroutines.launch

class AutoCheckHistoryActivity :BaseActivity() {

    private var projectId:Int = -1
    private var objectId:Int = -1
    private var stationId:Int = -1
    private val database = MonitoringRepository()

    override fun initView() {
        projectId = intent.getIntExtra(KEY_INTENT_PROJET_ID, -1)
        objectId = intent.getIntExtra(KEY_INTENT_OBJECT_ID, -1)
        stationId = intent.getIntExtra(KEY_INTENT_STATION_ID, -1)

        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.action_bar)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.action_bar)
        mComment_title?.setTitle("历史核验记录")

        val rv = findViewById<RecyclerView>(R.id.rv_history_checked)
        rv.layoutManager = LinearLayoutManager(this)
        val adapter = CollapsiblePointAdapter()
        rv.adapter = adapter

        lifecycleScope.launch {
            database.getCollectedPointsWithCheckedPointsByStationId(stationId).collect{
                val uiPoints = mutableListOf<UiPointWithCheckPoint>()
                it.forEach {
                    val uiPoint = UiPointWithCheckPoint(it, false)
                    uiPoints.add(uiPoint)
                }
                adapter.submitList(uiPoints)
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_autocheck_history
    }
}